package com.huanglj.himalaya.adapters;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.huanglj.himalaya.R;
import com.ximalaya.ting.android.opensdk.model.album.Album;
import com.ximalaya.ting.android.opensdk.model.word.QueryResult;

import java.util.ArrayList;
import java.util.List;

public class SearchResultAdapter extends RecyclerView.Adapter<SearchResultAdapter.InnerHolder> {
    private List<QueryResult> mResult = new ArrayList<>();
    private OnSearchItemClickListener mListener;

    @NonNull
    @Override
    public InnerHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View inflate = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_suggest_result, parent, false);
        return new InnerHolder(inflate);
    }

    @Override
    public void onBindViewHolder(@NonNull InnerHolder holder, final int position) {
        View itemView = holder.itemView;
        TextView resultTitle = itemView.findViewById(R.id.result_title);
        resultTitle.setText(mResult.get(position).getKeyword());
        resultTitle.setTag(position);
        resultTitle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mListener.onItemClick(position,mResult.get(position));
            }
        });
    }

    @Override
    public int getItemCount() {
        return mResult.size();
    }

    public void setData(List<QueryResult> result) {
        mResult.clear();
        mResult.addAll(result);
        notifyDataSetChanged();
    }

    public class InnerHolder extends RecyclerView.ViewHolder {
        public InnerHolder(@NonNull View itemView) {
            super(itemView);
        }
    }

    public void setOnSuggestItemClickListener(OnSearchItemClickListener listener){
        mListener = listener;
    }
    public interface OnSearchItemClickListener {
        void onItemClick(Integer tag, QueryResult result);
    }
}
